package cn.itcast.poi;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class App {

    // 导出
    @Test
    public void write() throws Exception {
        //创建工作簿，在内存中表示整个excel
        Workbook workbook = new XSSFWorkbook();
        //创建一个工作表
        Sheet sheet = workbook.createSheet();
        // 创建行
        Row row = sheet.createRow(0);
        // 创建单元格 (创建第一行第一列)
        Cell cell = row.createCell(0);
        // 设置单元格内容
        cell.setCellValue("第一行第一列");
        // 导出
        workbook.write(new FileOutputStream("e://test.xlsx"));
        workbook.close();
    }

    // 导入
    @Test
    public void read() throws Exception {
        //根据excel文件流，创建工作簿
        Workbook workbook = new XSSFWorkbook(new FileInputStream("e://test.xlsx"));
        //获取工作表
        Sheet sheet = workbook.getSheetAt(0);
        //获取行
        Row row = sheet.getRow(0);
        //获取单元格
        Cell cell = row.getCell(0);
        // 获取内容
        System.out.println("获取的内容：" + cell.getStringCellValue());
        System.out.println("获取总行数：" + sheet.getPhysicalNumberOfRows());
        System.out.println("获取行的总列数：" + row.getPhysicalNumberOfCells());
        workbook.close();
    }
}
















